# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
#
# CAUTION: Don't move this to the community repository until #12941 is fixed,
# so it can be built with cargo+rustc from aports!
pkgname=keepassxc-proxy-static
_projname=keepassxc-proxy-rust
pkgver=0_git20220628
_gitrev=bf4e913100bf4373ce207bc8c371de09b5be38ab
pkgrel=0
pkgdesc="A statically linked proxy between Native Messaging browser extension and KeePassXC (for use with Flatpak)"
url="https://github.com/varjolintu/keepassxc-proxy-rust"
arch="x86_64"  # limited by rustup, fails to build on aarch64
license="GPL-3.0-or-later"
makedepends="rustup"
install="$pkgname.post-install"
source="https://github.com/varjolintu/keepassxc-proxy-rust/archive/$_gitrev/$_projname-$_gitrev.tar.gz
	keepassxc-proxy-install.in
	"
builddir="$srcdir/$_projname-$_gitrev"
options="net !check"  # no tests provided

export CARGO_HOME="$srcdir/.cargo"
export RUSTUP_HOME="$srcdir/.rustup"

export CARGO_PROFILE_RELEASE_CODEGEN_UNITS=1
export CARGO_PROFILE_RELEASE_LTO="true"
export CARGO_PROFILE_RELEASE_OPT_LEVEL="z"
export CARGO_PROFILE_RELEASE_PANIC="abort"

prepare() {
	default_prepare

	sed "s/@@VERSION@@/$pkgver/" "$srcdir"/keepassxc-proxy-install.in \
		> "$srcdir"/keepassxc-proxy-install

	# TODO: This is a workaround for https://gitlab.alpinelinux.org/alpine/aports/-/issues/12941.
	# Remove it and build with cargo+rustc from aports (using `-C target-feature=+crt-static`)
	# after it's fixed.
	rustup-init --target "$CARCH-unknown-linux-musl" --profile minimal --no-modify-path -y
}

build() {
	PATH="$CARGO_HOME/bin:$PATH" cargo build --locked --release
}

package() {
	install -D -m755 target/release/keepassxc-proxy "$pkgdir"/usr/libexec/keepassxc-proxy.static
	install -D -m755 "$srcdir"/keepassxc-proxy-install "$pkgdir"/usr/bin/keepassxc-proxy-install
}

sha512sums="
cfb0229566c81df6604f91af6340684e6ff5877474b40e3247aedd37c21d5ab7121bb70dc38b68640848b7f0b11890f9b6c9e81ad316aa26adf9a26b21223ba4  keepassxc-proxy-rust-bf4e913100bf4373ce207bc8c371de09b5be38ab.tar.gz
3d31ed8d2a430f1e61960990128a9b7277106fcf74c16952ccefdeca3a3ba5cb9e6917d08d801bff1d4d354566689bda8f54d10f3711b03063d01374913c0387  keepassxc-proxy-install.in
"
